package eu.smoothit.sis.admin.backendBean;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import eu.smoothit.sis.admin.backendBean.superclass.DataTableAbstract;
import eu.smoothit.sis.admin.util.DTOComparator;
import eu.smoothit.sis.db.SisDAOFactory;
import eu.smoothit.sis.db.api.daos.ISwarmDAO;
import eu.smoothit.sis.db.impl.entities.PeerStatusEntry;
import eu.smoothit.sis.db.impl.entities.SwarmEntityEntry;
import eu.smoothit.sis.db.impl.entities.User;
import eu.smoothit.sis.db.impl.entities.UserRole;

public class MonitoringViewBean extends
		DataTableAbstract<SwarmEntityStatistics> {
	// Properties
	// ---------------------------------------------------------------------------------
	private static final Integer DEFAULT_TOP_Number = 10;
	// private SwarmEntityEntry searchExample;
	private Integer numberOfTop = DEFAULT_TOP_Number;
	private String selectedIoPParameter;

	private List<PeerStatusEntry> associatedIopListPerSwarm;
	// Number of available swarms
	private Integer swarmNrs;

	private List<PeerStatusEntry> associatedLocalPeersListPerSwarm;
	// define DAO
	SisDAOFactory factory = SisDAOFactory.getFactory();
	ISwarmDAO swarm_dao = factory.createSwarmDAO();

	private SwarmEntityStatistics swarmStatistics = new SwarmEntityStatistics();
	//if show details of related seeds for a swarm id
	private Boolean showDetailsTag=false;
	
	//related to associated iop, local peers per swarm
	private SwarmEntityEntry selectedSwarm;
	private List<PeerStatusEntry> associatedIoPs;
	private List<PeerStatusEntry> associatedLocaPeers;


	// initialization
	public MonitoringViewBean() {
		// searchExample = new SwarmEntityEntry();

		actionLoad();
	}

	// getter && setter
	// ----------------------------------------------------------------------------------
	public Boolean getShowDetailsTag() {
		return showDetailsTag;
	}

	public void setShowDetailsTag(Boolean showDetailsTag) {
		this.showDetailsTag = showDetailsTag;
	}


	public Integer getNumberOfTop() {
		return numberOfTop;
	}

	public void setNumberOfTop(Integer numberOfTop) {
		this.numberOfTop = numberOfTop;
	}

	// public SwarmEntityEntry getSearchExample() {
	// return searchExample;
	// }

	public String getSelectedIoPParameter() {
		return selectedIoPParameter;
	}

	public void setSelectedIoPParameter(String selectedIoPParamter) {
		this.selectedIoPParameter = selectedIoPParamter;
	}

	public List<PeerStatusEntry> getAssociatedIopListPerSwarm() {
		return associatedIopListPerSwarm;
	}

	public void setAssociatedIopListPerSwarm(
			List<PeerStatusEntry> associatedIopListPerSwarm) {
		this.associatedIopListPerSwarm = associatedIopListPerSwarm;
	}

	public List<PeerStatusEntry> getAssociatedLocalPeersListPerSwarm() {
		return associatedLocalPeersListPerSwarm;
	}

	public void setAssociatedLocalPeersListPerSwarm(
			List<PeerStatusEntry> associatedLocalPeersListPerSwarm) {
		this.associatedLocalPeersListPerSwarm = associatedLocalPeersListPerSwarm;
	}

	// public void setSearchExample(SwarmEntityEntry searchExample) {
	// this.searchExample = searchExample;
	// }

	public Integer getSwarmNrs() {
		return swarmNrs;
	}

	public void setSwarmNrs(Integer swarmNrs) {
		this.swarmNrs = swarmNrs;
	}
	public SwarmEntityEntry getSelectedSwarm() {
		return selectedSwarm;
	}

	public void setSelectedSwarm(SwarmEntityEntry selectedSwarm) {
		this.selectedSwarm = selectedSwarm;
	}

	public List<PeerStatusEntry> getAssociatedIoPs() {
		return associatedIoPs;
	}

	public void setAssociatedIoPs(List<PeerStatusEntry> associatedIoPs) {
		this.associatedIoPs = associatedIoPs;
	}
	public List<PeerStatusEntry> getAssociatedLocaPeers() {
		return associatedLocaPeers;
	}

	public void setAssociatedLocaPeers(List<PeerStatusEntry> associatedLocaPeers) {
		this.associatedLocaPeers = associatedLocaPeers;
	}
	// Form actions
	// ----------------------------------------------------------------------------------


	public void showDetails(){
		
		SwarmEntityStatistics temp_swarmEntityStatistics = (SwarmEntityStatistics) dataTable.getRowData();
		// swarms related info, TODO show it
		 selectedSwarm = temp_swarmEntityStatistics.getSwarm();
		//
		associatedIoPs=temp_swarmEntityStatistics.getAssociatedIopList();
		//
		 associatedLocaPeers=temp_swarmEntityStatistics.getAssociatedLocalPeerList();
		
//		List<String> swarmIDs = new ArrayList<String>();
//		for (SwarmEntityEntry r : swarms) {
//			swarmIDs.add(r.getId() + "|" + r.getTorrentHash());
//		}
//		setAssociatedRoleGroupRoleNames(roleNames);
//		setUsersAssociatedRoleGroupEditable(true);
		editModeRows.put(dataTable.getRowIndex(), true);
		 showDetailsTag=true;
	}
	public void hideDetails(){
		editModeRows.clear();
		showDetailsTag=false;
	}
	
	/**
	 * get top N entries based on selected filed
	 */
	public void actionFiltering() {
		dataTable.setRows(getNumberOfTop()); // top X
		sortField = getSelectedIoPParameter(); // "numberOfLeechers";

	}

	@Override
	public void actionRefresh() {
		// searchExample = new SwarmEntityEntry();
		super.actionRefresh();
	}

	@Override
	public void actionLoad() {
		try {
			List<SwarmEntityStatistics> swStatiticsList = new ArrayList<SwarmEntityStatistics>();
			List<SwarmEntityEntry> swarmEntities = swarm_dao.getAll();
			if (!swarmEntities.isEmpty()) {
				for (SwarmEntityEntry sw : swarmEntities) {
					swarmStatistics = new SwarmEntityStatistics();// otherwise
																	// not
																	// update
																	// date
					swarmStatistics.setSwarm(sw);
					swarmStatistics.statistics();
//					if (swarmStatistics.getAssociatedIopList() != null)
//						nrOfAssociatedIop = swarmStatistics
//								.getAssociatedIopList().size();
//					if (swarmStatistics.getAssociatedLocalPeerList() != null)
//						nrOfAssociatedLocalPeers = swarmStatistics
//								.getAssociatedLocalPeerList().size();
					swStatiticsList.add(swarmStatistics);
				}
				dataList = swStatiticsList;
				// nrOfAssociatedIop
				// nrOfAssociatedLocalPeers

				swarmNrs = dataList.size();
			}

		} catch (Exception e) {
			setErrorMessage(e.getMessage() + " Cause: " + e.getCause());
			e.printStackTrace();
			return;
		}
		// TODO sort!!
		sortByLocalPeerDesc();
//		actionSort(null);
//		try {
//			sortByLocalPeerDesc();
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}

	public void sortByLocalPeerDesc() {
		// Get and set sort field and sort order.
		String sortFieldAttribute = "nrOfAssociatedLocalPeers";
		sortField = sortFieldAttribute;
		sortAscending = false;
	if(dataList!=null && !dataList.isEmpty())
			Collections.sort(dataList, new DTOComparator(sortField,
					sortAscending));
		// Clear row modes.
		editModeRows.clear();
		selectedRows.clear();

	}

	@Override
	public void actionDelete() {
		// TODO Auto-generated method stub

	}

	@Override
	public void actionSave() {
		// TODO Auto-generated method stub

	}


//	public static void main(String args[]) {
//
//		monitoringViewBean ins = new monitoringViewBean();
//		ins.actionLoad();
//		SwarmEntityEntry inss;
//		if (ins.dataList != null && !ins.dataList.isEmpty()) {
//			for (SwarmEntityStatistics sm : ins.dataList) {
//				inss = new SwarmEntityEntry();
//				inss = sm.getSwarm();
//				System.err.println("swarm id: " + inss.getId()
//						+ inss.getAvailability());
////				sm.getAssociatedLocalPeerList()
//				
//			
//				System.err.println("Nr of local peers:"+	sm.getNrOfAssociatedLocalPeers()+"  "+sm.getNrOfAssociatedIop());
//				
//			}
////			System.err.println("Nr of local peers:"+ins.getNrOfAssociatedLocalPeers());
//		}
//	}

}
